<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="历史版本" prop="hisVersion">
        <el-select @change="changeHisVersion($event)" v-model="queryParams.hisVersion" placeholder="请选择历史版本号" clearable>
          <el-option
            v-for="dict in hisVersions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @keyup.enter.native="handleQuery"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模块名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模块名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-select v-model="queryParams.version" placeholder="请选择版本号" clearable>
          <el-option
            v-for="dict in dict.type.report_version"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态"  prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            @keyup.enter.native="handleQuery"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:modular:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table max-height="550px"
      v-if="refreshTable"
      v-loading="loading"
      :data="modularList"
       row-key="id"
      border="border"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column v-hasPermi="['reportworld:modular:query','reportworld:modular:add','reportworld:modular:edit','reportworld:modular:remove']"
        label="操作" align="center" type="" width="230px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row,false)"
            v-hasPermi="['reportworld:modular:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reportworld:modular:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['reportworld:modular:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reportworld:modular:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="模块名称" align="center" prop="name"  width="180px" :show-overflow-tooltip="true" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" v-if="false"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="版本号" width="150" align="center" prop="version">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.report_version" :value="scope.row.version"/>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加人" width="150" align="center" prop="createBy" :show-overflow-tooltip="true" />
      <el-table-column label="编辑时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编辑人" width="150" align="center" prop="updateBy" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />

    </el-table>

    <!-- 添加或修改模块对话框 -->
    <el-dialog :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级模块" prop="parentId">
              <treeselect v-model="form.parentId" :options="modularOptions" :normalizer="normalizer"
                          placeholder="请选择上级模块"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模块名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入模块名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-select :disabled="true" clearable v-model="form.version" placeholder="请选择版本号">
                <el-option
                  v-for="dict in dict.type.report_version"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input-number style="width: 100%" v-model="form.orderNum" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input maxlength="500" v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listModular, getModular, delModular, addModular, updateModular ,init} from "@/api/reportworld/modular";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Modular",
    dicts: ['sys_common_status','report_version'],
    components: {
      Treeselect
    },
    data() {
      return {
        butloading:false,
        isUpdate:false,
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 模块表格数据
        modularList: [],
        // 模块树选项
        modularOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否展开，默认全部展开
        isExpandAll: false,
        // 重新渲染表格状态
        refreshTable: true,
        hisVersions:[],
        // 查询参数
        queryParams: {
          name: null,
          status: null,
          hisVersion:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          parentId: [
            { required: true, message: "上级模块不能为空", trigger: "change" }
          ],
          name: [
            { required: true, message: "模块名称不能为空", trigger: "change" }
          ],
          orderNum: [
            { required: true, message: "显示顺序不能为空", trigger: "change" }
          ],
          version: [
            { required: true, message: "版本号不能为空", trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.init();
      this.getList();
    },
    methods: {
      changeHisVersion(value){
        this.queryParams.hisVersion=value;
        this.init();
        this.getList();
      },
      init(){
        init(this.queryParams).then(response => {
          this.hisVersions=response.hisVersions;
      });
      },
      /** 查询模块列表 */
      getList() {
        this.loading = true;
        listModular(this.queryParams).then(response => {
          this.modularList = this.handleTree(response.data, "id", "parentId");
          this.loading = false;
      });
      },
      /** 转换模块数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      /** 查询模块下拉树结构 */
      getTreeselect() {
        listModular().then(response => {
          this.modularOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.modularOptions.push(data);
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentId: null,
          name: null,
          orderNum: null,
          status: null,
          createTime: null,
          createBy: null,
          updateTime: null,
          updateBy: null,
          remark: null,
          version:null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.getTreeselect();
        if (row != null && row.id) {
          this.form.parentId = row.id;
        } else {
          this.form.parentId = 0;
        }
        this.open = true;
        this.title = "添加模块";
        this.isUpdate =true;
        this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
        this.form.version = this.dict.type.report_version[0].value;
        if(this.queryParams.hisVersion){
          this.form.version =this.queryParams.hisVersion;
        }
      },
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
      });
      },
      /** 修改按钮操作 */
      handleUpdate(row,isUpdate) {
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parentId = row.id;
        }
        this.queryParams.id=row.id;
        getModular(this.queryParams).then(response => {
          this.form = response.data;
        this.open = true;
        this.title = "修改模块";
        this.isUpdate =true;
        if(isUpdate == false){
          this.title = "模块详情";
          this.isUpdate =false;
        }
      });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.butloading=true;
            this.form.hisVersion =this.queryParams.hisVersion;
            if (this.form.id != null) {
              updateModular(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
            } else {
              addModular(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const hisVersion=this.queryParams.hisVersion;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delModular(row.id,hisVersion);
        }).then(() => {
          this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
      }
    }
  };
</script>
